/**
 * Created by zhouaqian on 2017/4/18.
 */
var React = require('react');

var Bheader = require('../widget/Bheader');
var Footer = require('../widget/Footer');
var GoodSwiper = require('../widget/GoodSwiper');
var ajax = require('../tools/ajax');
var IncreaseId = require('../../web/tools/IncreaseId');
var MsgBox = require('../components/MsgBox');
var AttrContent = require('../widget/AttrContent');

module.exports = React.createClass({
    getInitialState: function () {
        return {
            newProInfo:[]
        }
    },
    myScroll:"",

    componentDidMount: function () {
        var goods_id = localStorage.getItem("goods_id");
        this.getNewProInfo(goods_id);
        setTimeout(this.iscroll,500);
    },

    /**iScroll插件*/
    iscroll(){
        var p =this;
        p.myScroll = new IScroll("#pointGoodDetailWrapper",{
            // 默认iscroll会拦截元素的默认事件处理函数，我们需要响应onClick，因此要配置
            preventDefault: false,
            // 禁止缩放
            zoom: false,
            // 支持鼠标事件，因为我开发是PC鼠标模拟的
            mouseWheel: true,
            // 滚动事件的探测灵敏度，1-3，越高越灵敏，兼容性越好，性能越差
            probeType: 3,
            // 拖拽超过上下界后出现弹射动画效果，用于实现下拉/上拉刷新
            bounce: true,
            // 展示滚动条
            scrollbars: false
        });
    },

    /**获取新人专享商品信息*/
    getNewProInfo(goods_id){
        var p =this;
        ajax("POST","getNewProInfo",{goods_id:goods_id},function(dt){
            if(dt&&dt.success){
                var objs = dt.data[0];
                p.setState({newProInfo:objs});
                console.log(objs);
            }
        })
    },

    order(){
        var p = this;
        var order_id = IncreaseId.increaseId();
        var newProInfo = p.state.newProInfo;
        var userId = localStorage.getItem('userId');
        if(userId){
            ajax("POST", "addOrderGoods", {
                order_id: order_id,
                goods_id: newProInfo.id,
                shop_id: "",
                goods_name: newProInfo.goods_name,
                goods_price: newProInfo.newperson_price,
                goods_number: 1,
                goods_type: newProInfo.goods_type,
                goods_attr: newProInfo.goods_attr,
                good_tax_fee: newProInfo.good_tax_fee,
                is_discount: 0
            }, function (dt) {
                if (dt && dt.success) {
                    console.log("添加成功");
                }
            });
            var order_sn = p.orderSn();
            var add_time = new Date().getTime();
            ajax("POST", "addOrderInfo", {
                orderId: order_id,
                order_sn: order_sn,
                user_id: userId,
                order_status: 0,
                shipping_status: 0,
                pay_status: 0,
                goods_amount: newProInfo.newperson_price,
                shipping_fee: 0,
                order_amount: newProInfo.newperson_price,
                goods_tax: 0,
                add_time: add_time
            }, function (dt) {
                if (dt && dt.success) {
                    localStorage.setItem("orderId", order_id);
                    location.href = '#orderInfo';
                }
            });
        }else{
            p.props.MsgBox();
        }
    },
    /**生成order_sn*/
    orderSn(){
        var now = new Date();
        var year = now.getFullYear();       //年
        var month = now.getMonth() + 1;     //月
        var day = now.getDate();            //日
        var clock = year;
        if(month < 10) {
            clock = clock+ "0" + month;
        }else{
            clock +=month;
        }
        if(day < 10) {
            clock = clock+ "0" + day;
        }else{
            clock +=day;
        }
        var random ='';
        for(var i=0;i<6;i++){
            random += Math.floor(Math.random()*10);
        }
        clock += random;
        return(clock);
    },

    render: function () {
        var p = this;
        var newProInfo = p.state.newProInfo;
        if(newProInfo.goods_img){
            var goods_img = newProInfo.goods_img.split(",");
        }
        if(newProInfo.original_img){
            var i=-1;
            var imgList = newProInfo.original_img.split(",").map(function (obj) {
                i++;
                return (
                    <img key={i} src={obj}/>
                )
            });
        }
        return (
            <div className="main"  id="main">
                <Bheader title="商品详情" hact="#newExclusive" headr="false"></Bheader>
                <div id="pointGoodDetailWrapper" className="container-t wrap">
                    <div  style={{minHeight:"28.66rem"}} className="goods_detail">
                        <div className="goods_con">
                            <GoodSwiper ref="goodSwiper" goods_img={goods_img}></GoodSwiper>
                            <p className="goods_name" ref="goodname">
                                {newProInfo.goods_name}
                            </p>
                            <p className="goods_price">
                                ￥<span className="price">{newProInfo.newperson_price}</span>
                            </p>
                            <hr className="orderHr"></hr>
                            <div className="goods_info newGoodsInfo">
                                <p className="title">商品信息:</p>
                                <ul>
                                    <li className="left">
                                        品牌：<span className="infoBold">{newProInfo.brand_name}</span>
                                    </li>
                                    <li className="right">
                                        规格：<span className="infoBold">{newProInfo.goods_guige}</span>
                                    </li>
                                    <li className="left">
                                        产地：<span className="infoBold">{newProInfo.gc_name}</span>
                                    </li>
                                    <li className="right">
                                        库存：<span className="infoBold">{newProInfo.goods_number}</span>
                                    </li>
                                </ul>
                            </div>
                            <div className="pro-detail">
                                <p className="title">详情:</p>
                                {imgList}
                            </div>
                        </div>
                    </div>
                    <AttrContent ref="attrContent" jifen={p.state.jifen} MsgBox={p.MsgBox} fleshNum={p.fleshNum}></AttrContent>
                </div>
                <div className="goods_btn newBtn" onClick={p.order} data-id={newProInfo.id}>
                    <div className="goods_addcart">
                        立即购买
                    </div>
                </div>
                <MsgBox ref="MsgBox"></MsgBox>
            </div>
        )
    }
});